package com.pfzy.ui.activity.refresh;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.pfzy.ui.R;
import com.pfzy.ui.databinding.ActivityRefreshBinding;

public class RefreshActivity extends AppCompatActivity implements View.OnClickListener{
    private ActivityRefreshBinding mBind;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBind = ActivityRefreshBinding.inflate(getLayoutInflater());
        setContentView(mBind.getRoot());
        initView();
    }

    private void initView() {
        TextView title = mBind.toolbar.findViewById(R.id.layout_toolbar_title);
        title.setText("刷新");
        mBind.toolbar.findViewById(R.id.layout_toolbar_back).setOnClickListener(this);

        String[] array = new String[20];
        for (int i = 0; i < array.length; i++) {
            array[i] = "string " + i;
        }
        mBind.activityRefreshRecycler.setLayoutManager(new LinearLayoutManager(this));
        mBind.activityRefreshRecycler.setAdapter(new ArrayAdapter(this, array));

        mBind.swipeRefreshLayout.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mBind.swipeRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mBind.swipeRefreshLayout.setRefreshing(false);
                    }
                }, 1000);
            }
        });
        mBind.swipeRefreshLayout.setOnLoadListener(new PullRefreshLayout.OnLoadListener() {
            @Override
            public void onLoad() {
                mBind.swipeRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mBind.swipeRefreshLayout.setLoading(false);
                    }
                }, 1000);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_toolbar_back:
                finish();
                break;
            default:
                break;
        }
    }
}
